package com.zhrj.exam.excel;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;

public class IdConverter implements Converter<Long> {

    @Override
    public Class<?> supportJavaTypeKey() {
        // 实体类中对象属性类型
        return Long.class;
    }

    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        // Excel中对应的CellData属性类型
        return CellDataTypeEnum.STRING;
    }

    @Override
    public Long convertToJavaData(ReadCellData<?> cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        // 将 Excel 中读取的 String 转换为 Long
        String value = cellData.getStringValue();
        return value != null ? Long.parseLong(value) : null;
    }

    @Override
    public WriteCellData<?> convertToExcelData(Long value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        // 将 Long 转换为 String 避免科学计数法
        return value != null ? new WriteCellData<>(String.valueOf(value)) : new WriteCellData<>("");
    }
}
